package com.example.huwang.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.huwang.R;
import com.example.huwang.model.CaseBean;
import com.example.huwang.model.DefinitionBean;
import com.huwang.greendao.gen.CaseBeanDao;
import com.huwang.greendao.gen.DefinitionBeanDao;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

import static com.example.huwang.app.MainApplication.dbController;

public class MoreDetailActivity extends AppCompatActivity {
    @BindView(R.id.id_recycle_view)
    public RecyclerView recycleView;

    @BindView(R.id.title)
    public TextView title;

    @BindView(R.id.return_text)
    public TextView returnText;
    @BindView(R.id.return_btn)
    public ImageView returnBtn;


    private BaseQuickAdapter<CaseBean, BaseViewHolder> mAdapter;
    private List<CaseBean> data = new ArrayList();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_more_detail);
        ButterKnife.bind(this);
        returnText.setOnClickListener(v -> finish());
        returnBtn.setOnClickListener(v -> finish());
        initRecycleView();
    }

    private void initRecycleView() {
        Intent intent = getIntent();
        int type = 1;
        String title_content = "";
        String content = "";
        Long pic_num = null;
        if (intent != null) {
            type = intent.getIntExtra("type", 1);
            title_content = intent.getStringExtra("title");
            content = intent.getStringExtra("content");
            pic_num = intent.getLongExtra("pic_num", 0);
        }
        List<DefinitionBean> definitionBeans = dbController.getDefinitionBeanDao().queryBuilder().where(DefinitionBeanDao.Properties.Type.eq(type)).list();
        List<CaseBean> beans = dbController.getCaseBeanDao().queryBuilder().where(CaseBeanDao.Properties.Type.eq(type)).list();
        title.setText("案例");
        data.clear();
        data.addAll(beans);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        recycleView.setLayoutManager(manager);
        String finalTitle_content = title_content;
        String finalContent = content;
        Long finalPicNum = pic_num;
        recycleView.setAdapter(mAdapter = new BaseQuickAdapter<CaseBean, BaseViewHolder>(R.layout.item_layout_example_more, data) {

            @Override
            protected void convert(BaseViewHolder helper, CaseBean item) {
                helper.setText(R.id.id_rv_title, finalTitle_content);
                helper.setText(R.id.id_tv_content, finalContent);
                if (finalPicNum == 0) {
                    helper.setImageDrawable(R.id.more_pic, getResources().getDrawable(R.mipmap.zhapian_1));
                }else if(finalPicNum == 1){
                    helper.setImageDrawable(R.id.more_pic, getResources().getDrawable(R.mipmap.zhapian_2));
                }else if(finalPicNum == 2){
                    helper.setImageDrawable(R.id.more_pic, getResources().getDrawable(R.mipmap.zhapian_3));
                }else if(finalPicNum == 3){
                    helper.setImageDrawable(R.id.more_pic, getResources().getDrawable(R.mipmap.zhapian_4));
                }else if(finalPicNum == 4){
                    helper.setImageDrawable(R.id.more_pic, getResources().getDrawable(R.mipmap.zhapian_5));
                }else if(finalPicNum == 5){
                    helper.setImageDrawable(R.id.more_pic, getResources().getDrawable(R.mipmap.zhapian_6));
                }
            }
        });
    }
}

